import request from '@/utils/request'
import type {Child} from '@/types/child'
import {UnwrapRef} from "vue";

export interface ChildAndElderBind {
    childId: number
    elderId: number
}

// 定义查询参数类型
export interface ChildQueryParams {
    childName?: string;  // 子女姓名（模糊查询）
    phone?: string;      // 电话号码（模糊查询）
    communityId?: number; // 社区ID（精确匹配）
}

// 获取所有子女列表（支持条件查询）
export const findAllChild = (params?: ChildQueryParams) => {
    return request<Child[]>({
        url: '/child/findAllChild',
        method: 'get',
        params: params  // 将查询参数添加到请求中
    })
}

// 添加子女
export const addChild = (data: {
    password?: UnwrapRef<Partial<Child>["password"]>;
    phone?: UnwrapRef<Partial<Child>["phone"]>;
    childName?: UnwrapRef<Partial<Child>["childName"]>;
    communityName?: UnwrapRef<Partial<Child>["communityName"]>;
    id: number | undefined;
    avatar?: UnwrapRef<Partial<Child>["avatar"]>;
    communityId?: UnwrapRef<Partial<Child>["communityId"]>
}) => {
    return request({
        url: '/child/register',
        method: 'post',
        data
    })
}

// 更新子女信息
export const updateChild = (child: {
    password?: UnwrapRef<Partial<Child>["password"]>;
    phone?: UnwrapRef<Partial<Child>["phone"]>;
    childName?: UnwrapRef<Partial<Child>["childName"]>;
    communityName?: UnwrapRef<Partial<Child>["communityName"]>;
    id: number | undefined;
    avatar?: UnwrapRef<Partial<Child>["avatar"]>;
    communityId?: UnwrapRef<Partial<Child>["communityId"]>
}) => {
    return request({
        url: '/child/updateChild',
        method: 'post',
        data: child
    })
}

// 删除子女
export const delChild = (child: Child) => {
    return request({
        url: '/child/delChild',
        method: 'delete',
        data: child
    })
}

// 获取子女关联的老人
export const getChildAndElder = (childId: number) => {
    return request({
        url: '/child/getChildAndElder',
        method: 'get',
        params: {childId}
    })
}

// 绑定子女和老人
export const bindChildAndElder = (id: number, number: number) => {
    return request<boolean>({
        url: '/child/bindChildAndElder',
        method: 'post',
        data: {
            childId: id,
            elderId: number
        }
    })
}

// 解绑子女和老人
export const unbindChildAndElder = (data: ChildAndElderBind) => {
    return request({
        url: '/child/unbindChildAndElder',
        method: 'post',
        data
    })
} 